﻿using System;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Comissoes.Models;
using Comissoes.Security;

namespace Comissoes.Controllers
{
    public class HomeController : AppController
    {
        public ActionResult Index()
        {
            xcontEntities db = new xcontEntities();

            StringBuilder m = new StringBuilder();
            
            Int32? ID_PERF = Access.User.PERFIS.ID_PERF;

            try
            {
                m.AppendLine("<h3><a href=\"#\">Home</a></h3>");
                m.AppendLine("<div>");
                m.AppendLine("<ul class=\"accordion-menu\">");
                m.AppendLine("<li>");
                m.AppendLine("</li>");
                m.AppendLine("</ul>");
                m.AppendLine("</div>");

                foreach (MENUS menu in db.MENUS.OrderBy(t => t.ORDEM_MENU))
                {
                    var submenus = from c in db.SUBMENUS
                                   from d in c.FUNCOES_PROGRAMA.FUNCOES_PERFIL
                                   where (c.MENUS.ID_MENU == menu.ID_MENU) 
                                   && (d.PERFIS.ID_PERF == ID_PERF) 
                                   && (d.PERMITIDO_FNPF == "A")
                                   orderby c.ORDEM_SBMN
                                   select new
                                   {
                                       d.FUNCOES_PROGRAMA.ID_FNPR,
                                       d.FUNCOES_PROGRAMA.URL_FNPR,
                                       d.FUNCOES_PROGRAMA.VALOR_FNPR
                                   };

                    if (submenus.Any())
                    {
                        m.AppendFormat("<h3><a href=\"#\">{0}</a></h3>", menu.DESC_MENU);
                        m.AppendLine("<div>");
                        m.AppendLine("<ul class=\"accordion-menu\">");

                        foreach (var submenu in submenus)
                            m.AppendFormat("<li class=\"ui-widget ui-widget-content ui-corner-all\" onclick=\"loadOption('{0}')\"><a href=\"#\" class=\"ui-icon ui-icon-triangle-1-e\"></a>{1}</li>", submenu.URL_FNPR, submenu.VALOR_FNPR);

                        m.AppendLine("</ul>");
                        m.AppendLine("</div>");
                    }
                }

                m.AppendLine("<h3><a href=\"#\">Sair</a></h3>");
                m.AppendLine("<div>");
                m.AppendLine("<ul class=\"accordion-menu\">");
                m.AppendLine("<li class=\"ui-widget ui-widget-content ui-corner-all\" style=\"text-align: center;\">");
                m.AppendLine("Deseja sair do sistema?<br /><br />");
                m.AppendLine("<button type=\"button\" class=\"onlytextcommand\" onclick=\"window.location = '/Acesso/Login'\">Sim</button>");
                m.AppendLine("&nbsp;&nbsp;&nbsp;&nbsp;");
                m.AppendLine("<button type=\"button\" class=\"onlytextcommand\">Não</button><br />");
                m.AppendLine("</li>");
                m.AppendLine("</ul>");
                m.AppendLine("</div>");

                ViewData["Menu"] = m.ToString();

                return View();
            }
            catch (Exception ex)
            {
                String message = ex.Message;
                if (ex.InnerException != null)
                    message += ex.InnerException.Message;

                return RedirectToAction("Error", new { message = message });
            }
        }

        public ActionResult Error(String message)
        {
            ViewData["message"] = message;

            return View();
        }

        public ActionResult Main()
        {
            return View();
        }

        public ActionResult Menu()
        {
            return PartialView();
        }
    }
}
